AFAIK, it is exactly the same as populating a ListView from a SQLite
database, except:
1. You put a Spinner widget in the layout instead of a ListView
2. You call setDropDownResource() on your adapter to define the layout
to be used when the user is making a choice from the Spinner
3. You use Spinner-friendly layouts with the adapter, such as
android.R.layout.simple_spinner_item in the constructor and
android.R.layout.simple_spinner_dropdown_item in the
setDropDownResource() call
I have never used a Spinner with SQLite, simply because if there is
enough data to warrant SQLite, there is too much data to use with a
Spinner, IMHO.
> Please, I'm about to pull my hair out.
Speaking as a follicle-ly challenged individual, I cannot recommend this
practice. ;-)
--
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy
Android App Developer Books: http://commonsware.com/books
I *definitely* would learn Java basics first outside of Android.
> In the 6 books I'm reading, every author uses ListView in their
> example code. And then says something like, spinner is the same. Well
> no, it isn't.
By and large, it is. Your problem isn't with Spinner, near as I can
tell. It's with CursorAdapter, and secondarily with a sub-optimal tutorial.
> And here is the class where I'm stumped, in the newView function. I'm
> trying to adapt your newView code from a ListView adapter to a Spinner
> adapter:
>
> // Adapter for cboModel
> class ModelAdapter extends CursorAdapter {
> ModelAdapter(Cursor c) {
> super(Main.this, c);
> }
>
> @Override
> public void bindView(View row, Context ctxt, Cursor c) {
> PkmodelWrapper wrapper=(PkmodelWrapper)row.getTag();
> wrapper.populateFrom(c);
> }
>
> -----------> here is where I'm lost:
>
> @Override
> public View newView(Context ctxt, Cursor c, ViewGroup parent) {
> ArrayAdapter<String> adapterForSpinner;
> adapterForSpinner = new ArrayAdapter<String>
> (this,android.R.layout.simple_spinner_item);
> adapterForSpinner.setDropDownViewResource
> (android.R.layout.simple_spinner_dropdown_item);
> cboModel.setAdapter(adapterForSpinner);
>
> PkmodelWrapper wrapper = new PkmodelWrapper(row);
> row.setTag(wrapper);
> wrapper.populateFrom(c);
> return(row);
> }
> }
Just have newView() inflate a new row:
@Override
public View newView(Context ctxt, Cursor c, ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.whateveryoucallit, parent, false);
PkmodelWrapper wrapper=new PkmodelWrapper(row);
row.setTag(wrapper);
return(row);
}
(substituting in the proper layout ID for whatever your custom layout is
for the drop-down resource)
After newView() returns, bindView() will be called to pour the contents
of your Cursor into that row.
Now, _Android Programming Tutorials_, tutorial #12, step #6 shows
newView() doing the binding as well. At the time I wrote that tutorial,
I thought newView() was also responsible for binding, which it isn't.
The next version of the book (should be available in digital form by the
end of the year) will correct that. What's in the tutorial isn't wrong,
per se, but it is redundant and unnecessary.
--
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy
Android Consulting/App Development: http://commonsware.com/consulting
getLayoutInflater() is a method on Activity, I believe. inflate() is a
method on LayoutInflater.
> I'm reading some java books too, Head First Java seems the most
> accessible, any others you would recommend?
I learned Java long enough ago that I think the books I used were
written on papyrus. But _Head First Java_ seems to be the best-reviewed
of the current crop.
> Yes, I should have subscribed online instead of buying your book
> through Amazon, kicking myself now.
Don't do that -- it might leave bruises that would be tough to explain. ;-)
--
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy
Android 1.6 Programming Books: http://commonsware.com/books
Make sure your query is including the _id column.
Also, a slight optimization: the value of c.getColumnIndex("Drug") will
not change in the lifetime of a Cursor, so you might see if you can
cache it somehow.
--
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy
_Android Programming Tutorials_ Version 1.0 Available!